package id.ac.amikom.amikomsocial;

import id.ac.amikom.libs.AdemDBHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AdemViewMateriActivity extends Activity {

	private AdemDBHelper db = null;
	private String mkl;
	private String dosen;
	int _id;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.adem_materi);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.adem_title);

		Bundle extras = getIntent().getExtras();
		_id = Integer.parseInt(extras.getString("_id"));						

		TextView viewJudul = (TextView) findViewById(R.id.materi_title);
		TextView viewDosen = (TextView) findViewById(R.id.materi_dosen);
		TextView viewMkl = (TextView) findViewById(R.id.materi_mkl);
		TextView viewFile = (TextView) findViewById(R.id.materi_file);
		TextView viewDate = (TextView) findViewById(R.id.materi_date);
		TextView viewDetail = (TextView) findViewById(R.id.materi_detail);
		TextView viewSize = (TextView) findViewById(R.id.materi_size);
		Button btnDown = (Button) findViewById(R.id.btnDownload);

		ImageView viewImage = (ImageView) findViewById(R.id.materi_image);

		String title = "";
		dosen = "";
		mkl = "";
		String file = "";
		String date = "";
		String detail = "";
		String size = "";

		db = new AdemDBHelper(this);
		Cursor cr = db.getDetailMateri(_id);

		if (cr.moveToFirst()) {
			do {

				title = cr.getString(cr.getColumnIndex("judul"));
				dosen = cr.getString(cr.getColumnIndex("dosen"));
				mkl = cr.getString(cr.getColumnIndex("mkl"));
				file = cr.getString(cr.getColumnIndex("file"));
				date = cr.getString(cr.getColumnIndex("upload"));
				detail = cr.getString(cr.getColumnIndex("sinopsis"));
				size = cr.getString(cr.getColumnIndex("size"));

			} while (cr.moveToNext());

		}

		// Log.i("judul", title);

		String[] f = file.split("[/]");
		String t[] = f[2].split("[.]");
		String type = t[t.length - 1];
		final String fl = file;

		viewJudul.setText(title);
		viewDosen.setText("Lecturer. " + dosen);
		viewMkl.setText("Subject. " + mkl);
		viewFile.setText("File. " + f[2]);
		viewDate.setText("Date. " + date);
		viewSize.setText("Size. " + size);
		viewDetail.setText("Sinopsis. " + detail);

		if (type.equals("doc") || type.equals("docx"))
			viewImage.setImageResource(R.drawable.i_word);
		else if (type.equals("xls") || type.equals("xlsx"))
			viewImage.setImageResource(R.drawable.i_excel);
		else if (type.equals("ppt") || type.equals("pptx"))
			viewImage.setImageResource(R.drawable.i_ppt);
		else if (type.equals("pdf"))
			viewImage.setImageResource(R.drawable.i_pdf);
		else
			viewImage.setImageResource(R.drawable.i_other);

		cr.close();
		db.close();

		btnDown.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {

				String url = "http://dosen.amikom.ac.id/downloads/materi/" + fl;
				Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
				startActivity(i);

				Toast.makeText(
						AdemViewMateriActivity.this,
						"Starting download file, check status on notification bar",
						Toast.LENGTH_LONG).show();

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.adem_menu_explore, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.id_exp_subj:
			Intent i = new Intent(AdemViewMateriActivity.this,
					AdemExploreMateriActivity.class); 
			i.putExtra("param", mkl);	  
			i.putExtra("mode", "mkl");
	        startActivity(i);	        			
			finish();			
			return true;

		case R.id.id_exp_lect:
			Intent in = new Intent(AdemViewMateriActivity.this,
					AdemExploreMateriActivity.class); 
			in.putExtra("param", dosen);
			in.putExtra("mode", "dosen");
	        startActivity(in);	        			
			finish();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}

	}

}
